package com.manlost.biz.tag.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manlost.biz.baby.model.constants.BabyType;
import com.manlost.biz.tag.BackTagService;
import com.manlost.biz.tag.impl.dao.interfaces.BabyTagAccountRelationDAO;
import com.manlost.biz.tag.impl.dao.interfaces.TagDAO;
import com.manlost.biz.tag.impl.dao.interfaces.TagGroupDAO;
import com.manlost.biz.tag.model.constants.TagInfoStatus;
import com.manlost.biz.tag.model.dataobject.Tag;
import com.manlost.biz.tag.model.dataobject.TagGroup;
import com.manlost.biz.tag.model.dto.TagDTO;
import com.manlost.biz.tag.model.dto.TagGroupDTO;
import com.manlost.biz.tag.query.CountBabyTagAccountRelationQuery;
import com.manlost.biz.tag.query.ListTagGroupQuery;
import com.manlost.biz.tag.query.ListTagQuery;

/**
 * 类TagServiceImpl.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-3-29 下午10:58:03
 */
@Service
public class BackTagServiceImpl implements BackTagService {

    @Autowired
    private TagDAO                    tagDAO;
    @Autowired
    private TagGroupDAO               tagGroupDAO;
    @Autowired
    private BabyTagAccountRelationDAO babyTagAccountRelationDAO;

    public List<TagGroupDTO> listTagGroupDTO() {
        ListTagGroupQuery query = new ListTagGroupQuery(TagInfoStatus.ENABLE);
        List<TagGroup> tagGroups = listTagGroup(query);
        List<TagGroupDTO> tagGroupDTOs = new ArrayList<TagGroupDTO>();
        for (TagGroup tagGroup : tagGroups) {
            TagGroupDTO dto = new TagGroupDTO();
            dto.setTagGroupId(tagGroup.getId());
            dto.setTagGroupName(tagGroup.getName());

            ListTagQuery tagQuery = new ListTagQuery(tagGroup.getId(), TagInfoStatus.ENABLE);
            List<Tag> tags = listTag(tagQuery);
            Long videoCount = 0l;
            Long picCount = 0l;
            List<String> tagNames = new ArrayList<String>();
            for (Tag tag : tags) {
                tagNames.add(tag.getName());
                videoCount = videoCount
                             + babyTagAccountRelationDAO.countBabyByQuery(new CountBabyTagAccountRelationQuery(
                                                                                                               tag.getId(),
                                                                                                               BabyType.VIDEO.getValue()));
                picCount = picCount
                           + babyTagAccountRelationDAO.countBabyByQuery(new CountBabyTagAccountRelationQuery(
                                                                                                             tag.getId(),
                                                                                                             BabyType.PIC.getValue()));
            }
            dto.setTagGroupVideoCount(videoCount);
            dto.setTagGroupPicCount(picCount);
            dto.setTagNames(tagNames);

            tagGroupDTOs.add(dto);
        }
        return tagGroupDTOs;
    }

    public List<TagDTO> listTagDTO() {
        ListTagQuery query = new ListTagQuery(TagInfoStatus.ENABLE);
        List<Tag> tags = listTag(query);
        List<TagDTO> tagDTOs = new ArrayList<TagDTO>();
        for (Tag tag : tags) {
            TagDTO dto = new TagDTO();
            dto.setTagId(tag.getId());
            dto.setTagName(tag.getName());
            dto.setTagVideoCount(babyTagAccountRelationDAO.countBabyByQuery(new CountBabyTagAccountRelationQuery(
                                                                                                                 tag.getId(),
                                                                                                                 BabyType.VIDEO.getValue())).longValue());
            dto.setTagPicCount(babyTagAccountRelationDAO.countBabyByQuery(new CountBabyTagAccountRelationQuery(
                                                                                                               tag.getId(),
                                                                                                               BabyType.PIC.getValue())).longValue());
            TagGroup tagGroup = tagGroupDAO.findById(tag.getTagGroupId());
            if (tagGroup != null) {
                dto.setTagGroupName(tagGroup.getName());
            }
            tagDTOs.add(dto);
        }
        return tagDTOs;
    }

    private List<Tag> listTag(ListTagQuery query) {
        if (!query.validate()) {
            throw new IllegalArgumentException("listTag, query validate failed");
        }
        return tagDAO.listTag(query);
    }

    private List<TagGroup> listTagGroup(ListTagGroupQuery query) {
        if (!query.validate()) {
            throw new IllegalArgumentException("listTagGroup, query validate failed");
        }
        return tagGroupDAO.listTagGroup(query);
    }

    public Tag addTag(Tag record) {
        return tagDAO.insert(record);
    }

    public TagGroup addTagGroup(TagGroup record) {
        if (record == null || record.getId() != null) {
            throw new IllegalArgumentException("addTagGroup, record is null || record id is not null");
        }
        return tagGroupDAO.insert(record);
    }

    public void disableTagGroup(Long tagGroupId) {
        tagGroupDAO.disableTagGroupById(tagGroupId);
    }

}
